<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of capitalAction
 *
 * @author wcj
 */
class capital_statusAction extends backendAction {

    public function _initialize() {
        parent::_initialize();
        $this->_mod = D('z');
        $this->_date =date("Y-m-d");
//        $this->_date ='2017-07-28';
        $this->tgroup = [100 => '普通用户', 1 => '总经销商', 2 => '经销商', 3 => '代理商', 4 => '经纪人'];
    }

    public function index() {
        $map = $this->_search();
        $mod = $this->_mod;
        $date=$this->_date;
        if (!empty($mod)) {
            $result = $this->_list($mod, $map);
            $this->assign('page', $result['page']);

            if ($this->_request('export') != '') {
                $list = $mod->where($map)->select();
            } else {
                $list = $result['list'];
            }

            foreach ($list as $key => $val) {
                $list[$key]['tgroup_name'] = get_tgroup($val['tgroup']);
                $list[$key]['tgroup_1_name'] = get_username($val['tgroup_1']);
                $list[$key]['tgroup_2_name'] = get_username($val['tgroup_2']);
                $list[$key]['tgroup_3_name'] = get_username($val['tgroup_3']);
                $list[$key]['tgroup_4_name'] = get_username($val['tgroup_4']);
                
                $list[$key]['now_price'] = get_now_amount($val['uid'], $date);
    

            }

            if ($this->_request('export') != '') {
                Vendor('excelClass.excelclass');
                $excel = new excelClass();
                $excel->echocapitalStatus('今日资金状况表' . date('YmdHis') . '.xls', $list);
                exit;
            }
//            return print_r($list);
            $this->assign('list', $list);


            $list2 = $mod->where($map)->select();
//              foreach ($list2 as $key => $val) {
//                $list2[$key]['tgroup_name'] = get_tgroup($val['tgroup']);
//                $list2[$key]['tgroup_1_name'] = get_username($val['tgroup_1']);
//                $list2[$key]['tgroup_2_name'] = get_username($val['tgroup_2']);
//                $list2[$key]['tgroup_3_name'] = get_username($val['tgroup_3']);
//                $list2[$key]['tgroup_4_name'] = get_username($val['tgroup_4']);
//                $list2[$key][ $val['action']] = $val['price'];
//                $list2[$key]['update_status'] = get_update_status($val['id']);
//                $list2[$key]['order_status'] = get_order_status($val['status']);
//                $list2[$key]['topkey'] = get_topkey($val['uid']);
//                $list2[$key]['now_price'] = get_now_amount($val['uid'],$val['add_time']);
//                $list2[$key]['lastDay_price'] = get_lastDay_amount($val['uid'],$val['add_time']);
//            }

            foreach ($list2 as $key => $val) {
                
                $list2[$key]['now_price'] = get_now_amount($val['uid'], $date);
//                $list2[$key]['now_price'] = $val['price'];

                
          
            }
            
            foreach ($list2 as $key => $val) {
            
                $sum_recharge += $val['recharge'];
                $sum_cash += $val['cash'];
                $sum_order += $val['order1'];
                $sum_order_refund += $val['order_refund'];
                $sum_express += $val['express'];
                $sum_commission += $val['commission'];
                $sum_now_amount += $val['now_price'];
                
                }
            if( $_GET['tgroup_5']!=''){
            $sum_price_=D('user')->where((array('tgroup_5' => $_GET['tgroup_5'])))->sum('price');
            }
            
            elseif($_GET['tgroup_4']!=''&&$_GET['tgroup_5']==''){
            $sum_price_=D('user')->where((array('tgroup_4' => $_GET['tgroup_4'])))->sum('price');
            }
             elseif($_GET['tgroup_3']!=''&&$_GET['tgroup_4']==''){
            $sum_price_=D('user')->where((array('tgroup_3' => $_GET['tgroup_3'])))->sum('price');
            }
              elseif($_GET['tgroup_2']!=''&&$_GET['tgroup_3']==''){
            $sum_price_=D('user')->where((array('tgroup_2' => $_GET['tgroup_2'])))->sum('price');
            }
                elseif($_GET['tgroup_1']!=''&&$_GET['tgroup_2']==''){
            $sum_price_=D('user')->where((array('tgroup_1' => $_GET['tgroup_1'])))->sum('price');
            }
            
            else{
            $sum_price_=D('user')->sum('price');

            }
            $this->assign('sum_price_', $sum_price_);
            $list1 = D('team')->field('title')->select();
            $arr = i_array_column_ext($list1, 'title');
            $this->assign('topkey', $arr);
            $this->assign('sum', [$sum_now_amount,  $sum_recharge, $sum_cash, $sum_order, $sum_order_refund, $sum_express, $sum_commission]);
            $this->assign('lottery', [0 => '未升级', 1 => '升级中', 2 => '升级成功', 9 => '升级失败']);
            $this->assign('status', [100 => '未付款', 1 => '已付款', 2 => '已发货', 3 => '已确认', 4 => '已评价', 5 => '已成功', 7 => '待处理退款', 8 => '已退款', 9 => '已取消']);
            $this->display();
        }
    }

    protected function _search() {
        $map = array();
        ($uid = $this->_request('uid', 'trim')) && $map['uid'] = $uid;
        ($keyword = $this->_request('keyword', 'trim')) && $map['uid'] = array('IN', $this->_get_uids($keyword, 'username'));
        ($tele = $this->_request('tele', 'trim')) && $map['uid'] = array('IN', $this->_get_uids($tele, 'tele'));
        ($realname = $this->_request('realname', 'trim')) && $map['uid'] = array('IN', $this->_get_uids($realname, 'realname'));
        // ($remark = $this->_request('remark', 'trim')) && $map['uid'] = array('IN',$this->_get_uids($remark,'remark'));
        //7.15
        if ($_GET['action']) {
            $action = $_GET['action'];
            if ($_GET['action'] != -1) {
                $map['action'] = $_GET['action'];
            }

            $this->assign('action_', $action);
        }

        if ($_GET['tgroup']) {
            $tgroup = $_GET['tgroup'];
            $tgroup = ($_GET['tgroup'] != 100) ? $_GET['tgroup'] : 0;
            $map['tgroup'] = $tgroup;
        }

        if ($_GET['lottery'] && $_GET['lottery'] != -1) {
            $lottery = $_GET['lottery'];
            $map['lottery'] = $tgroup;
        }

        if ($_GET['status'] && $_GET['status'] != -1) {
            $status = $_GET['status'];
            $status = ($_GET['status'] != 100) ? $_GET['status'] : 0;
            $map['status'] = $status;
        }

        if ($_GET['topkey']) {
            $topkey = $_GET['topkey'];
            $map['topkey'] = $topkey;
        }
        if ($_GET['tele']) {
            $topkey = $_GET['tele'];
            $map['tele'] = $tele;
        }
        
         if ($_GET['tgroup_1']) {
            $map['tgroup_1'] = $_GET['tgroup_1'];
        }
           if ($_GET['tgroup_2']) {
            $map['tgroup_2'] = $_GET['tgroup_2'];
        }
            if ($_GET['tgroup_3']) {
            $map['tgroup_3'] = $_GET['tgroup_3'];
        }
            if ($_GET['tgroup_4']) {
            $map['tgroup_4'] = $_GET['tgroup_4'];
        }
        
               if ($_GET['tgroup_5']) {
            $map['tgroup_5'] = $_GET['tgroup_5'];
        }
        

        if ($_GET['stime']) {
            $time_start = $_GET['stime'] . ' 00:00:00';
            $time_end = $_GET['etime'] . ' 23:59:59';
            $map['ktime'] = array(array('egt', $time_start), array('elt', $time_end));
        }
        //7.15
//            $map['username']='一念';
        $this->assign('search', array(
            'keyword' => $keyword,
            'tele' => $_GET['tele'],
            'realname' => $realname,
            'tgroup' => $_GET['tgroup'],
            'tgroup_1' => get_username($_GET['tgroup_1']),
            'tgroup_2' => get_username($_GET['tgroup_2']),
            'tgroup_3' => get_username($_GET['tgroup_3']),
            'tgroup_4' =>get_username( $_GET['tgroup_4']),
            'tgroup_5' => get_username($_GET['tgroup_5']),
            'topkey' => $_GET['topkey'],
            'status' => $status,
            'type' => $type[1],
            'lottery' => $_GET['lottery'],
            'status' => $_GET['status'],
            'stime' => $_GET['stime'],
            'etime' => $_GET['etime'],
        ));


        return $map;
    }

    public function _get_uids($keyword, $field) {
        $where[$field] = array('like', '%' . $keyword . '%');
        $uidarr = D('user')->where($where)->field('id')->select();
        $_idarr = array();
        foreach ($uidarr as $v) {
            $_idarr[] = $v['id'];
        }
        return implode(',', $_idarr);
    }

    public function insert($date = '2017-07-26') {
//            $date='2017-07-17';
        set_time_limit(0);
        $list = D('user')->select();
        foreach ($list as $key => $val) {
            $list[$key]['now_price'] = get_now_amount($val['id'], $date);
            $list[$key]['cash'] = get_now_cash($val['id'], $date);
            $list[$key]['recharge'] = get_now_recharge($val['id'], $date);
            $list[$key]['order'] = get_now_order($val['id'], $date);
            $list[$key]['order_refund'] = get_now_order_refund($val['id'], $date);
            $list[$key]['commission'] = get_now_commission($val['id'], $date);
            $list[$key]['express'] = get_now_express($val['id'], $date);
        }


        foreach ($list as $key => $val) {
            $data = array(
                'username' => $val['username'],
                'uid' => $val['id'],
                 'tele' => $val['tele'],
                'tgroup' => $val['tgroup'],
                'tgroup_1' => $val['tgroup_1'],
                'tgroup_2' => $val['tgroup_2'],
                'tgroup_3' => $val['tgroup_3'],
                'topkey' => $val['topkey'],
                'ktime' => $date,
                'now_price' => $val['now_price'],
                'cash' => $val['cash'],
                'recharge' => $val['recharge'],
                'order1' => $val['order'],
                'order_refund' => $val['order_refund'],
                'commission' => $val['commission'],
                'express' => $val['express'],
            );
            $dd = D('commission_summary')->add($data);
        }
    }

    
   
    function test() {

        $i = get_now_amount(364, '2017-07-28');
         $j= get_now_amount(5, '2017-07-17');
        echo $i;
    }
    

}
